from PyQt6.QtWidgets import (QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
                               QMenuBar, QMenu, QDialog, QLabel,
                               QTimeEdit, QDoubleSpinBox, QGridLayout,
                               QPushButton, QStackedWidget, QFrame)
from PyQt6.QtCore import Qt, QTime
from PyQt6.QtGui import QFont
from salary_timer import SalaryTimer
from styles import (MAIN_WINDOW_STYLE, SETTINGS_FORM_STYLE, 
                   SAVE_BUTTON_STYLE)

class SettingsDialog(QWidget):
    """设置页面"""
    def __init__(self, salary_timer, parent=None):
        super().__init__(parent)
        self.salary_timer = salary_timer
        self.init_ui()
        
    def init_ui(self):
        layout = QVBoxLayout()
        layout.setContentsMargins(30, 30, 30, 30)
        layout.setSpacing(20)
        
        # 标题
        title = QLabel("设置")
        title.setFont(QFont("Microsoft YaHei", 18, QFont.Weight.Bold))
        title.setStyleSheet("color: #24292e;")
        layout.addWidget(title)
        
        # 设置表单
        form = QWidget()
        form.setStyleSheet(SETTINGS_FORM_STYLE)
        form_layout = QGridLayout(form)
        form_layout.setVerticalSpacing(15)
        form_layout.setHorizontalSpacing(15)
        
        # 工作时间设置
        form_layout.addWidget(QLabel("上班时间"), 0, 0)
        self.start_time = QTimeEdit(self.salary_timer.start_time)
        form_layout.addWidget(self.start_time, 0, 1)
        
        form_layout.addWidget(QLabel("下班时间"), 1, 0)
        self.end_time = QTimeEdit(self.salary_timer.end_time)
        form_layout.addWidget(self.end_time, 1, 1)
        
        # 休息时间设置
        form_layout.addWidget(QLabel("休息开始时间"), 2, 0)
        self.break_start = QTimeEdit(self.salary_timer.break_start)
        form_layout.addWidget(self.break_start, 2, 1)
        
        form_layout.addWidget(QLabel("休息结束时间"), 3, 0)
        self.break_end = QTimeEdit(self.salary_timer.break_end)
        form_layout.addWidget(self.break_end, 3, 1)
        
        # 日薪设置
        form_layout.addWidget(QLabel("日薪(元)"), 4, 0)
        self.daily_salary = QDoubleSpinBox()
        self.daily_salary.setRange(0, 100000)
        self.daily_salary.setValue(self.salary_timer.daily_salary)
        form_layout.addWidget(self.daily_salary, 4, 1)
        
        layout.addWidget(form)
        
        # 保存按钮
        save_button = QPushButton("保存设置")
        save_button.setFixedSize(100, 32)
        save_button.setStyleSheet(SAVE_BUTTON_STYLE)
        save_button.clicked.connect(self.save_settings)
        
        button_container = QWidget()
        button_layout = QHBoxLayout(button_container)
        button_layout.addWidget(save_button)
        button_layout.addStretch()
        
        layout.addWidget(button_container)
        layout.addStretch()
        
        self.setLayout(layout)
    
    def save_settings(self):
        """保存设置"""
        self.salary_timer.start_time = self.start_time.time()
        self.salary_timer.end_time = self.end_time.time()
        self.salary_timer.break_start = self.break_start.time()
        self.salary_timer.break_end = self.break_end.time()
        self.salary_timer.daily_salary = self.daily_salary.value()
        self.salary_timer.save_settings()

class MainWindow(QMainWindow):
    """主窗口"""
    def __init__(self):
        super().__init__()
        self.setWindowTitle("工作计时器")
        self.setWindowFlags(Qt.WindowType.WindowStaysOnTopHint)  # 窗口置顶
        self.setStyleSheet(MAIN_WINDOW_STYLE)
        self.init_ui()
        
    def init_ui(self):
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 创建主布局
        main_layout = QHBoxLayout(central_widget)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.setSpacing(0)
        
        # 创建左侧导航栏
        nav_bar = QFrame()
        nav_bar.setObjectName("nav_bar")
        nav_bar.setFixedWidth(180)  # 增加导航栏宽度
        nav_layout = QVBoxLayout(nav_bar)
        nav_layout.setContentsMargins(0, 10, 0, 0)
        nav_layout.setSpacing(2)
        
        # 创建导航按钮
        self.timer_btn = QPushButton("计时器")
        self.timer_btn.setObjectName("nav_button")
        self.timer_btn.setCheckable(True)
        self.timer_btn.setChecked(True)
        
        self.settings_btn = QPushButton("设置")
        self.settings_btn.setObjectName("nav_button")
        self.settings_btn.setCheckable(True)
        
        nav_layout.addWidget(self.timer_btn)
        nav_layout.addWidget(self.settings_btn)
        nav_layout.addStretch()
        
        # 创建右侧内容区
        content_widget = QWidget()
        content_layout = QVBoxLayout(content_widget)
        content_layout.setContentsMargins(0, 0, 0, 0)
        content_layout.setSpacing(0)
        
        # 创建堆叠式页面
        self.stack = QStackedWidget()
        
        # 创建计时器页面
        timer_page = QWidget()
        timer_layout = QVBoxLayout(timer_page)
        timer_layout.setContentsMargins(0, 0, 0, 0)
        self.salary_timer = SalaryTimer()
        timer_layout.addWidget(self.salary_timer)
        
        # 创建设置页面
        settings_page = QWidget()
        settings_layout = QVBoxLayout(settings_page)
        settings_layout.setContentsMargins(0, 0, 0, 0)
        self.settings_widget = SettingsDialog(self.salary_timer, self)
        settings_layout.addWidget(self.settings_widget)
        
        # 添加页面到堆叠式窗口
        self.stack.addWidget(timer_page)
        self.stack.addWidget(settings_page)
        
        content_layout.addWidget(self.stack)
        
        # 连接按钮信号
        self.timer_btn.clicked.connect(lambda: self.switch_page(0))
        self.settings_btn.clicked.connect(lambda: self.switch_page(1))
        
        # 添加导航栏和内容区到主布局
        main_layout.addWidget(nav_bar)
        main_layout.addWidget(content_widget)
        
        # 设置窗口大小和位置
        self.setMinimumSize(800, 600)
        self.setGeometry(100, 100, 800, 600)
    
    def switch_page(self, index):
        """切换页面"""
        self.stack.setCurrentIndex(index)
        self.timer_btn.setChecked(index == 0)
        self.settings_btn.setChecked(index == 1) 